package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.activity.parent;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.R;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.databinding.ActivityHomeParentBinding;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.GlobalViewModelStore;
import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils.MyApplication;
import com.google.android.material.bottomnavigation.BottomNavigationView;

// 家长端主界面
public class ActivityHomeParent extends AppCompatActivity {

    private ActivityHomeParentBinding binding;
    private UserViewModel userViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityHomeParentBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //用户身份
        setUserViewModel();

        setBottomNavigationView();

    }

    private void setUserViewModel(){
        //用户身份
        // 获取全局共享的 UserViewModel
        userViewModel = GlobalViewModelStore
                .getInstance(MyApplication.getInstance())
                .get(UserViewModel.class);
        //
        userViewModel.setUserType("parent");
    }

    private void setBottomNavigationView() {
        BottomNavigationView navView = findViewById(R.id.nav_view);

        // 创建一个 AppBarConfiguration 对象，用于配置操作栏
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home_parent,
                R.id.navigation_community,
                R.id.navigation_chat,
                R.id.navigation_ai,
                R.id.navigation_persionalcenter,
                R.id.navigation_medicinereminder
        ).build();

        // 通过 Navigation.findNavController 方法获取导航控制器实例
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_home_parent);

        // 将导航控制器与操作栏关联起来
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

        // 将导航控制器与底部导航栏关联起来
        NavigationUI.setupWithNavController(navView, navController);

        // 使用新的 setOnItemSelectedListener 方法
        navView.setOnItemSelectedListener(item -> {
            if (item.getItemId() == R.id.navigation_home_parent) {
                // 重置导航栈
                navController.popBackStack(R.id.navigation_home_parent, false);
            }
            return NavigationUI.onNavDestinationSelected(item, navController);
        });
    }
}